package com.caifeng.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class VfenzuAdapter  extends BaseExpandableListAdapter {

        private List<VfenzuGroupBean> mList;
        private LayoutInflater inflater;

        public VfenzuAdapter(List<VfenzuGroupBean> mList, Context context) {
            this.mList = mList;
            this.inflater = LayoutInflater.from(context);
        }

        @Override
        public int getGroupCount() {
            return mList == null ? 0 : mList.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return mList.get(groupPosition).getmList().size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return mList.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return mList.get(groupPosition).getmList().get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        /**
         *
         */

        public class ViewHolderGroup {
            private ImageView imageViewIconIndicator;
            private TextView gr_name, gr_online;
        }

        public class ViewHolderChild {
            private ImageView ch_pic;
            private TextView ch_name, ch_online, ch_text;
        }


        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

            ViewHolderGroup viewHolderGroup = null;
            if (convertView == null) {
                viewHolderGroup = new ViewHolderGroup();
                convertView = inflater.inflate(R.layout.two_fenzu_group, parent, false);
                viewHolderGroup.imageViewIconIndicator = convertView.findViewById(R.id.gr_pic);
                viewHolderGroup.gr_name = convertView.findViewById(R.id.gr_title);
                viewHolderGroup.gr_online = convertView.findViewById(R.id.gr_online);
                convertView.setTag(viewHolderGroup);

            } else {
                viewHolderGroup = (ViewHolderGroup) convertView.getTag();
            }
            VfenzuGroupBean tencentGroupBean = mList.get(groupPosition);
            if (isExpanded) {
                viewHolderGroup.imageViewIconIndicator.setImageResource(R.mipmap.dnow);

            } else {
//            viewHolderGroup.gr_name.setText(tencentGroupBean.getGr_title());
                viewHolderGroup.imageViewIconIndicator.setImageResource(R.mipmap.you);
            }
            viewHolderGroup.gr_name.setText(tencentGroupBean.getGr_title());
            viewHolderGroup.gr_online.setText(tencentGroupBean.getGr_online()
                    + "/" + tencentGroupBean.getGr_totalPerson());
            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

            ViewHolderChild viewHolderChild = null;
            if (convertView == null) {
                viewHolderChild = new ViewHolderChild();
                convertView=inflater.inflate(R.layout.two_fenzu_child,parent,false);
                VfenzuGroupBean.VfenzuChildBean tencentChildBean =
                        mList.get(groupPosition).getmList().get(childPosition);

                viewHolderChild.ch_pic = convertView.findViewById(R.id.ch_pic);
                viewHolderChild.ch_text = convertView.findViewById(R.id.ch_text);
                viewHolderChild.ch_name = convertView.findViewById(R.id.ch_name);
                viewHolderChild.ch_online = convertView.findViewById(R.id.ch_online);
                convertView.setTag(viewHolderChild);

            } else {
                viewHolderChild = (ViewHolderChild) convertView.getTag();
            }

            VfenzuGroupBean.VfenzuChildBean tencentChildBean =
                    mList.get(groupPosition).getmList().get(childPosition);

            viewHolderChild.ch_pic.setImageResource(tencentChildBean.getAvatarid());
            viewHolderChild.ch_name.setText(tencentChildBean.getCh_name());
            viewHolderChild.ch_online.setText(tencentChildBean.getCh_online());
            viewHolderChild.ch_text.setText(tencentChildBean.getCh_text());


            return convertView;
        }


        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    }
